package org.spring.qq_chat_java.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.spring.qq_chat_java.constant.ReceiverType;
import org.spring.qq_chat_java.pojo.Result;
import org.spring.qq_chat_java.pojo.User;

import org.spring.qq_chat_java.pojo.friend;
import org.spring.qq_chat_java.pojo.uto.chatListUTO;

import org.spring.qq_chat_java.service.FriendService;
import org.spring.qq_chat_java.service.UserService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/friends")
@CrossOrigin(origins = "*")
@RequiredArgsConstructor
public class FriendController {
    private final FriendService friendService;
    private final UserService userService;



    @GetMapping
    public Result<List<chatListUTO>> getFriends(@RequestParam Long id) {
        //根据userId查询friend
        List<friend> friends = friendService.list(new QueryWrapper<friend>().eq("user_id",id));

        //根据friendId查询user
        List<chatListUTO> friendUTO = friends.stream().map(friend -> {
            User user = userService.getById(friend.getFriendId());
            return new chatListUTO(user.getId(),user.getAvatar(), user.getUsername(), friend.getFriendGroup(), ReceiverType.user,  0);
        }).toList();
        return Result.success(friendUTO);



    }
}
